//
//  ZORulerScrollView.m
//  WaxPatch
//
//  Created by 梁炜东 on 16/4/20.
//  Copyright © 2016年 zerotech.com. All rights reserved.
//

#import "ZYXRulerScrollView.h"
#import <math.h>

@interface ZYXRulerScrollView ()
@property (nonatomic, assign)CGFloat distanceBottom;
@end

@implementation ZYXRulerScrollView

- (void)setRulerValue:(CGFloat)rulerValue
{
    _rulerValue = rulerValue;
}

- (void)drawRuler
{
    NSArray *subViews = [self subviews];
    for (int i = 0; i < subViews.count; i++)
    {
        UILabel *label = subViews[i];
        [label removeFromSuperview];
        label = nil;
    }
    
    CGMutablePathRef pathRef1 = CGPathCreateMutable();
    CGMutablePathRef pathRef2 = CGPathCreateMutable();
    
    CAShapeLayer *shapeLayer1 = [CAShapeLayer layer];
    shapeLayer1.strokeColor = [UIColor lightGrayColor].CGColor;
    shapeLayer1.fillColor = [UIColor clearColor].CGColor;
    shapeLayer1.lineWidth = 2.f;
    shapeLayer1.lineCap = kCALineCapButt;
    
    CAShapeLayer *shapeLayer2 = [CAShapeLayer layer];
    shapeLayer2.strokeColor = [UIColor grayColor].CGColor;
    shapeLayer2.fillColor = [UIColor clearColor].CGColor;
    shapeLayer2.lineWidth = 2.f;
    shapeLayer2.lineCap = kCALineCapButt;
    for (int i = 0; i <= self.rulerCount; i++)
    {
        UILabel *rule = [[UILabel alloc] init];
        NSInteger nMaxWidth = 2048;
        NSInteger nMaxHeight = 1536;
        CGRect tbBounds = [[UIScreen mainScreen] bounds];
        CGFloat fScaleW = tbBounds.size.width / nMaxWidth;
        CGFloat fScaleH = tbBounds.size.height / nMaxHeight;
        CGFloat fScale = fScaleW > fScaleH ? fScaleH : fScaleW;
        CGFloat fontPt = ceil(80 * fScale);
//        CGFloat fontPt = 2.0;
        self.distanceBottom = ceil(30 * fScale);
        
//        rule.font = [UIFont fontWithName:@"HelveticaNeue" size:fontPt];
        rule.font = [UIFont italicSystemFontOfSize:fontPt];
        rule.textColor = [UIColor whiteColor];
        if (i == 0)
        {
            rule.text = self.arrayText[i];
        }else if(i%10 == 0)
        {
            int j = i/10;
            rule.text = self.arrayText[j];
        }
        CGSize textSize = [rule.text sizeWithAttributes:@{ NSFontAttributeName : rule.font }];
        if (i % 10 == 0)
        {
            CGPathMoveToPoint(pathRef2, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * i ,self.distanceBottom + fontPt);
            
            CGPathAddLineToPoint(pathRef2, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * i, self.rulerHeight);
            
            rule.frame = CGRectMake(DISTANCELEFTANDRIGHT + DISTANCEVALUE * i - textSize.width / 2, self.rulerHeight/2-fontPt/2, 0, 0);
            
            [rule sizeToFit];
            [self addSubview:rule];
        }
        else
        {
            CGPathMoveToPoint(pathRef1, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * i , self.distanceBottom + fontPt + ceil(20 * fScale));

            CGPathAddLineToPoint(pathRef1, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * i, self.rulerHeight);
        }
    }
    
    shapeLayer1.path = pathRef1;
    shapeLayer2.path = pathRef2;
    shapeLayer1.hidden = YES;
    [self.layer addSublayer:shapeLayer1];
    [self.layer addSublayer:shapeLayer2];
    
    self.frame = CGRectMake(0, 0, self.rulerWidth, self.rulerHeight);
    // 开启最小模式
    if (_mode)
    {
        UIEdgeInsets edge = UIEdgeInsetsMake(0, self.rulerWidth / 2.f - DISTANCELEFTANDRIGHT, 0, self.rulerWidth / 2.f - DISTANCELEFTANDRIGHT);
        self.contentInset = edge;
        self.contentOffset = CGPointMake(DISTANCEVALUE * (self.rulerValue / [self.rulerAverage floatValue]) - self.rulerWidth + (self.rulerWidth / 2.f + DISTANCELEFTANDRIGHT), 0);
    }
    else
    {
        self.contentOffset = CGPointMake(DISTANCEVALUE * (self.rulerValue / [self.rulerAverage floatValue]) - self.rulerWidth / 2.f + DISTANCELEFTANDRIGHT, 0);
    }
    
    self.contentSize = CGSizeMake(self.rulerCount * DISTANCEVALUE + DISTANCELEFTANDRIGHT * 2.f, self.rulerHeight);
//    self.backgroundColor = [UIColor redColor];
}

- (void)reset
{
    if (_mode) {
        UIEdgeInsets edge = UIEdgeInsetsMake(0, self.rulerWidth / 2.f - DISTANCELEFTANDRIGHT, 0, self.rulerWidth / 2.f - DISTANCELEFTANDRIGHT);
        self.contentInset = edge;
        self.contentOffset = CGPointMake(DISTANCEVALUE * (self.rulerValue / [self.rulerAverage floatValue]) - self.rulerWidth + (self.rulerWidth / 2.f + DISTANCELEFTANDRIGHT), 0);
    }
    else
    {
        self.contentOffset = CGPointMake(DISTANCEVALUE * (self.rulerValue / [self.rulerAverage floatValue]) - self.rulerWidth / 2.f + DISTANCELEFTANDRIGHT, 0);
    }
}

@end
